<?php
/**
*
* @package MoltX QuickChat
* @author $Author: moltendorf $
* @version $Id: molten_chat_send.php 169 2009-09-26 05:23:57Z moltendorf $
* @latest $URL: https://molten-chat.googlecode.com/svn/trunk/source/molten_chat_send.php $
* @copyright (c) 2005-2009 Matthew Oltendorf
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
// Specify the path to you phpBB3 installation directory.
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
// The common.php file is required.
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin(false);
$auth->acl($user->data);

$user->setup();

// Get the message.
$message = request_var('message', '', true);
$message = utf8_normalize_nfc($message);
$message = htmlspecialchars_decode($message, ENT_COMPAT);

// Limit message length.
$message = utf8_substr($message, 0, (int) $config['molten_chat_cache_message_length']);

if (defined('molten_chat_enabled') and $message != '' and $auth->acl_get('u_molten_chat_user_send'))
{
	$username = htmlspecialchars_decode($user->data['username'], ENT_COMPAT);

	$color = true;
	$length = strlen ($user->data['user_colour']);

	if ($length > 0)
	{
		if ($length == 6)
		{
			$color = array(
				'red' => hexdec($user->data['user_colour'][0].$user->data['user_colour'][1]),
				'green' => hexdec($user->data['user_colour'][2].$user->data['user_colour'][3]),
				'blue' => hexdec($user->data['user_colour'][4].$user->data['user_colour'][5])
			);
		}
		elseif ($length == 3)
		{
			$color = array(
				'red' => hexdec($user->data['user_colour'][0].$user->data['user_colour'][0]),
				'green' => hexdec($user->data['user_colour'][1].$user->data['user_colour'][1]),
				'blue' => hexdec($user->data['user_colour'][2].$user->data['user_colour'][2])
			);
		}
	}

	$instance = request_var('instance', 'new');

	// Only registered users are handled server-side.
	if ($user->data['is_registered'] and $instance != 'new')
	{
		// Update user's time.
		if (!molten_chat_server::update($username, false, array('identifier' => $user->session_id, 'instance' => $instance), $color))
		{
			// Literally stop executing the file.
			return;
		}
	}
	else
	{
		// Literally stop executing the file.
		return;
	}

	// Send the message.
	molten_chat_server::send($message, $username, false);
}